]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
tests: Add extensionUtils unit test
authorFlorian Müllner <fmuellner@gnome.org>
Sun, 21 Jan 2024 23:44:40 +0000 (00:44 +0100)
committerMarge Bot <marge-bot@gnome.org>
Wed, 16 Oct 2024 16:16:08 +0000 (16:16 +0000)
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3379>

13 files changed:
tests/meson.build
tests/unit/extensionUtils.js [new file with mode: 0644]
tests/unit/fixtures/extensions/empty/.gitignore [new file with mode: 0644]
tests/unit/fixtures/extensions/invalid-shell-version1/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/invalid-shell-version2/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/invalid-uuid/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/invalid/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/missing-description/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/missing-name/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/missing-shell-version/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/missing-uuid/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/valid/metadata.json [new file with mode: 0644]
tests/unit/fixtures/extensions/wrong-uuid/metadata.json [new file with mode: 0644]

index 2b59303f264638bfb3fe589d797b12a2341033ca..a8f49a7ab5ff02fde8e6faedcb73b7681df05bb3 100644 (file)
@@ -20,6 +20,7 @@ unit_testenv.append('GI_TYPELIB_PATH', shell_typelib_path, separator: ':')
 unit_testenv.append('GI_TYPELIB_PATH', st_typelib_path, separator: ':')
 
 unit_tests = [
+    'extensionUtils',
     'highlighter',
     'injectionManager',
     'insertSorted',
diff --git a/tests/unit/extensionUtils.js b/tests/unit/extensionUtils.js
new file mode 100644 (file)
index 0000000..b54797f
--- /dev/null
@@ -0,0 +1,141 @@
+import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
+
+import 'resource:///org/gnome/shell/ui/environment.js';
+import * as ExtensionUtils from 'resource:///org/gnome/shell/misc/extensionUtils.js';
+
+const fixturesDir = Gio.File.new_for_uri(`${import.meta.url}/../fixtures/extensions`);
+
+describe('loadExtensionMetadata()', () => {
+    const {loadExtensionMetadata} = ExtensionUtils;
+
+    it('fails if directory name does not match requested UUID', () => {
+        const dir = fixturesDir.get_child('valid');
+        expect(() => loadExtensionMetadata('invalid', dir))
+            .toThrowError(/does not match UUID/);
+    });
+
+    it('fails if metadata.json is missing', () => {
+        const dir = fixturesDir.get_child('empty');
+        expect(() => loadExtensionMetadata('empty', dir))
+            .toThrowError(/Missing metadata/);
+    });
+
+    it('fails if metadata.json is not valid JSON', () => {
+        const dir = fixturesDir.get_child('invalid');
+        expect(() => loadExtensionMetadata('invalid', dir))
+            .toThrowError(/parse metadata/);
+    });
+
+    it('fails if metadata.json misses "uuid" property', () => {
+        const dir = fixturesDir.get_child('missing-uuid');
+        expect(() => loadExtensionMetadata('missing-uuid', dir))
+            .toThrowError(/missing "uuid"/);
+    });
+
+    it('fails if metadata.json misses "name" property', () => {
+        const dir = fixturesDir.get_child('missing-name');
+        expect(() => loadExtensionMetadata('missing-name', dir))
+            .toThrowError(/missing "name"/);
+    });
+
+    it('fails if metadata.json misses "description" property', () => {
+        const dir = fixturesDir.get_child('missing-description');
+        expect(() => loadExtensionMetadata('missing-description', dir))
+            .toThrowError(/missing "description"/);
+    });
+
+    it('fails if metadata.json misses "shell-version" property', () => {
+        const dir = fixturesDir.get_child('missing-shell-version');
+        expect(() => loadExtensionMetadata('missing-shell-version', dir))
+            .toThrowError(/missing "shell-version"/);
+    });
+
+    it('fails if metadata.json "uuid" property is not a string', () => {
+        const dir = fixturesDir.get_child('invalid-uuid');
+        expect(() => loadExtensionMetadata('invalid-uuid', dir))
+            .toThrowError(/"uuid" is not of type/);
+    });
+
+    it('fails if metadata.json "shell-version" property is not an array', () => {
+        const dir = fixturesDir.get_child('invalid-shell-version1');
+        expect(() => loadExtensionMetadata('invalid-shell-version1', dir))
+            .toThrowError(/"shell-version" is not of type/);
+    });
+
+    it('fails if metadata.json "shell-version" property does not contain strings', () => {
+        const dir = fixturesDir.get_child('invalid-shell-version2');
+        expect(() => loadExtensionMetadata('invalid-shell-version2', dir))
+            .toThrowError(/"shell-version" is not of type/);
+    });
+
+    it('fails if metadata.json "uuid" property does not match directory name', () => {
+        const dir = fixturesDir.get_child('wrong-uuid');
+        expect(() => loadExtensionMetadata('wrong-uuid', dir))
+            .toThrowError(/does not match directory/);
+    });
+
+    it('loads valid metadata.json', () => {
+        const dir = fixturesDir.get_child('valid');
+        expect(() => loadExtensionMetadata('valid', dir)).not.toThrow();
+    });
+});
+
+describe('serializeExtension()', () => {
+    const {
+        loadExtensionMetadata, serializeExtension, deserializeExtension, ExtensionType,
+    } = ExtensionUtils;
+
+    // based on ExtensionManager
+    function createExtensionObject(uuid, dir, type) {
+        const metadata = loadExtensionMetadata(uuid, dir);
+        const extension = {
+            metadata,
+            uuid,
+            type,
+            dir,
+            path: dir.get_path(),
+            error: '',
+            hasPrefs: dir.get_child('prefs.js').query_exists(null),
+            enabled: false,
+            hasUpdate: false,
+            canChange: false,
+            sessionModes: metadata['session-modes'] ?? ['user'],
+        };
+        return extension;
+    }
+    const uuid = 'valid';
+    const ext = createExtensionObject(uuid,
+        fixturesDir.get_child(uuid), ExtensionType.PER_USER);
+    let serialized;
+
+    beforeAll(() => {
+        jasmine.addCustomEqualityTester((file1, file2) => {
+            if (file1 instanceof Gio.File && file2 instanceof Gio.File)
+                return file1.equal(file2);
+            return undefined;
+        });
+    });
+
+    it('produces output that can be used as variant of the expected type', () => {
+        expect(() => {
+            serialized = serializeExtension(ext);
+        }).not.toThrow();
+
+        let v;
+        expect(() => {
+            v = new GLib.Variant('a{sv}', serialized);
+        }).not.toThrow();
+
+        expect(v.is_of_type(new GLib.VariantType('a{sv}'))).toBeTrue();
+    });
+
+    it('produces output that can be deserialized', () => {
+        let deserialized;
+        expect(() => {
+            deserialized = deserializeExtension(serialized);
+        }).not.toThrow();
+
+        expect(deserialized).toEqual(ext);
+    });
+});
diff --git a/tests/unit/fixtures/extensions/empty/.gitignore b/tests/unit/fixtures/extensions/empty/.gitignore
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/unit/fixtures/extensions/invalid-shell-version1/metadata.json b/tests/unit/fixtures/extensions/invalid-shell-version1/metadata.json
new file mode 100644 (file)
index 0000000..8730957
--- /dev/null
@@ -0,0 +1,6 @@
+{
+  "uuid": "invalid-shell-version1",
+  "name": "Some Name",
+  "description": "Some Description",
+  "shell-version": "45"
+}
diff --git a/tests/unit/fixtures/extensions/invalid-shell-version2/metadata.json b/tests/unit/fixtures/extensions/invalid-shell-version2/metadata.json
new file mode 100644 (file)
index 0000000..db786d9
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "uuid": "invalid-shell-version2",
+  "name": "Some Name",
+  "description": "Some Description",
+  "shell-version": [
+    45,
+    46
+  ]
+}
diff --git a/tests/unit/fixtures/extensions/invalid-uuid/metadata.json b/tests/unit/fixtures/extensions/invalid-uuid/metadata.json
new file mode 100644 (file)
index 0000000..684601c
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "uuid": {},
+  "name": "Some Name",
+  "description": "Some Description",
+  "shell-version": [
+    "45",
+    "46"
+  ]
+}
diff --git a/tests/unit/fixtures/extensions/invalid/metadata.json b/tests/unit/fixtures/extensions/invalid/metadata.json
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/unit/fixtures/extensions/missing-description/metadata.json b/tests/unit/fixtures/extensions/missing-description/metadata.json
new file mode 100644 (file)
index 0000000..15c7871
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "uuid": "missing-description",
+  "name": "Some Name",
+  "shell-version": [
+    "45",
+    "46"
+  ]
+}
diff --git a/tests/unit/fixtures/extensions/missing-name/metadata.json b/tests/unit/fixtures/extensions/missing-name/metadata.json
new file mode 100644 (file)
index 0000000..eb4ff5d
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "uuid": "missing-name",
+  "description": "Some Description",
+  "shell-version": [
+    "45",
+    "46"
+  ]
+}
diff --git a/tests/unit/fixtures/extensions/missing-shell-version/metadata.json b/tests/unit/fixtures/extensions/missing-shell-version/metadata.json
new file mode 100644 (file)
index 0000000..b3f288f
--- /dev/null
@@ -0,0 +1,5 @@
+{
+  "uuid": "missing-shell-version",
+  "name": "Some Name",
+  "description": "Some Description"
+}
diff --git a/tests/unit/fixtures/extensions/missing-uuid/metadata.json b/tests/unit/fixtures/extensions/missing-uuid/metadata.json
new file mode 100644 (file)
index 0000000..dce5f6c
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "name": "Some Name",
+  "description": "Some Description",
+  "shell-version": [
+    "45",
+    "46"
+  ]
+}
diff --git a/tests/unit/fixtures/extensions/valid/metadata.json b/tests/unit/fixtures/extensions/valid/metadata.json
new file mode 100644 (file)
index 0000000..3252aa1
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "uuid": "valid",
+  "name": "Some Name",
+  "description": "Some Description",
+  "shell-version": [
+    "45",
+    "46"
+  ]
+}
diff --git a/tests/unit/fixtures/extensions/wrong-uuid/metadata.json b/tests/unit/fixtures/extensions/wrong-uuid/metadata.json
new file mode 100644 (file)
index 0000000..c65ab86
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "uuid": "some-uuid",
+  "name": "Some Name",
+  "description": "Some Description",
+  "shell-version": [
+    "45",
+    "46"
+  ]
+}